'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:ChooseuserIndexCtrl
 * @description
 * # 用户选择人员页面
 * # 用户需要提供选过的人exclusionUsers字段，用来回显
 * # 数据接口路径由$routeParams.url获取
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('ChooseuserIndexCtrl', function(DataService, Util, $rootScope, openApi, localStorageService, $routeParams) {
        var vm = this;
        this.peoplelist = [];
        $rootScope.userArr = [];
        var choosedUsers = localStorageService.get("chooseUsrs");
        var userskey = localStorageService.get("userskey");
        localStorageService.remove("userskey");
        this.handleUser = function(userObj) {
            if (userObj.selected) {
                userObj.flag = 1
                $rootScope.userArr.push(userObj)
            } else {
                userObj.flag = 0
                $rootScope.userArr = $rootScope.userArr.filter(function(item, index) {
                    return item.id != userObj.id
                })
            }
            $rootScope.$broadcast("userArrchange")
        };
        // 获取转换后的userobj
        var getproxyusers = function() {
            // @id,name
            // 如果需要包装则包装，不然直接取值
            if (userskey && choosedUsers) {
                var keys = userskey;
                var userarr = [];
                choosedUsers.forEach(function(data, index) {
                    var userObj = {};
                    for (var key in keys) {
                        userObj[key] = data[keys[key]];
                    }
                    userarr.push(userObj);
                })
                return userarr
            } else {
                return choosedUsers
            }
        };
        // 导出转换后的userobj
        var exportproxyusers = function() {
            // @id,name
            // 如果需要包装则包装，不然直接返回值
            if (userskey) {
                var keys = userskey;
                var userarr = [];
                $rootScope.userArr.forEach(function(data, index) {
                    var userObj = {};
                    for (var key in keys) {
                        userObj[keys[key]] = data[key];
                    }
                    userarr.push(userObj);
                })
                return userarr
            } else {
                return $rootScope.userArr
            }
        };
        var init = function() {
            var param = {};
            var users = getproxyusers();
            if (users) {
                param.exclusionUsers = users.map(function(item, index) {
                    return item.id
                })
            }
            DataService.getData(openApi[$routeParams.url], param).then(function(data) {
                vm.peoplelist = data
                data.forEach(function(item, index) {
                    item.children.forEach(function(item, index) {
                        // 设置选中状态
                        if (item.flag == 1) {
                            item.selected = true;
                        }
                    });
                });
                // 底部选中回显
                if (users) {
                    $rootScope.userArr = users;
                    $rootScope.$broadcast("userArrchange");
                }
            })
        }
        this.confirm = function() {
            var users = exportproxyusers();
            localStorageService.set("chooseUsrs", users);
            Util.goBack();
        }
        init()
    });